---
id: 502
title: 脚本常见问题
---

import useBaseUrl from "@docusaurus/useBaseUrl";
import Tag from "@site/src/components/Tag.js";
import Highlight from '@site/src/components/Highlight.js';


## 一、脚本示例

```

//变量脚本动态设置传输实体的Demo
//只传输变量名称，变量值，变量在线状态，变量值改变时间
    public class DemoScript:IDynamicModel
    {
        public IEnumerable<dynamic> GetList(IEnumerable<dynamic> datas)
        {
            List<DemoData> demoDatas = new List<DemoData>();
            foreach (var data in datas)
            {
                DemoData demoData = new DemoData();
                demoData.Value = data.Value;
                demoData.Name = data.Name;
                demoData.IsOnline = data.IsOnline;
                demoData.ChangeTime = data.ChangeTime;
                demoDatas.Add(demoData);
            }
            return demoDatas;
        }
    }
    public class DemoData
    {
        public string Name { get; set; }
        public bool IsOnline { get; set; }
        public object Value { get; set; }
        public DateTime ChangeTime { get; set; }
    }

```


## 二、适配阿里云物模型脚本


对应文档：[阿里云设备属性上报](https://help.aliyun.com/zh/iot/user-guide/data-formats?spm=a2c4g.11186623.0.i17#section-jrb-lrl-b2b)



### 变量脚本
```

//一个适配阿里云IOT的变量脚本demo


public class AliYunIotScript:IDynamicModel
{
   public IEnumerable<dynamic> GetList(IEnumerable<dynamic> datas)
    {
        List<AliYunIot> aliYunIots = new();
        // 对输入列表进行分组，根据 Remark1属性分组
        var groups = datas.Where(a => !string.IsNullOrEmpty(a.Remark1)).GroupBy(a => a.Remark1);
        // 遍历每一个分组
        foreach (var item in groups)
        {
            var requestId = Yitter.IdGenerator.YitIdHelper.NextId();
            var iotId = item.Key; //Remark1自定义为设备Id
            var productKey = item.FirstOrDefault(a => !string.IsNullOrEmpty(a.Remark2))?.Remark2;//Remark2自定义为产品Id
            var deviceName = item.FirstOrDefault(a => !string.IsNullOrEmpty(a.Remark3))?.Remark3;//Remark3自定义为设备名称
            AliYunIot aliYunIot = new();
            aliYunIot.iotId = iotId;
            aliYunIot.requestId = requestId.ToString();
            aliYunIot.deviceName = deviceName;
            aliYunIot.productKey = productKey;
            // 遍历分组内的每一个元素
            foreach (var varItem in item)
            {

                    // 设置采集时间，并转换为 Unix 时间戳
                    // 设置值
                    var data = new Property() { value = varItem.Value, time = new DateTimeOffset(varItem.CollectTime).ToUnixTimeSeconds() };
                    // 以 变量名称 作为键，将新对象添加到分组的 属性 中
                    aliYunIot.items.Add(varItem.Name, data);
 
            }
            aliYunIots.Add(aliYunIot);
        }
        return aliYunIots;
    }

   
}
 public class AliYunIot
    {
        public string iotId { get; set; }
        public string requestId { get; set; }
        public string productKey { get; set; }
        public string deviceName { get; set; }
        public Dictionary<string, Property> items { get; set; } = new();
    }

    public class Property
    {
        public object value { get; set; }
        public long time { get; set; }
    }



```


### 传输主题

变量Topic 填入 `/${productKey}/${deviceName}/thing/event/property/post`
